package com.hss.rxjava;


import com.hss.rxjava.Interceptor.HeaderInterceptor;
import com.hss.rxjava.Interceptor.LoggerInterceptor;
import com.hss.rxjava.https.HttpsUtils;
import com.hss.rxjava.https.SSLParams;

import java.io.InputStream;

import okhttp3.OkHttpClient;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;


/**
 * Created by Administrator on 2016/12/4.
 */

public class ApiManager {
    public static ApiManager mApiManager = null;
    OkHttpClient okHttpClient;
    OkHttpClient.Builder okHttpBuilder;
    Api mApi;
    private HeaderInterceptor mHeaderInterceptor;

    public ApiManager() {
    }

    public static synchronized  ApiManager getInstance(){
        if (mApiManager == null){
            mApiManager = new ApiManager();
        }
        return  mApiManager;
    }
    private static final int TIMEOUT_READ = 25;
    private static final int TIMEOUT_CONNECTION = 25;
    public void init(InputStream ins,String pwd){
        if (null == mApi){

            okHttpBuilder = new OkHttpClient.Builder();
//
            mHeaderInterceptor = new HeaderInterceptor();
            okHttpBuilder.addInterceptor(mHeaderInterceptor);
            okHttpBuilder.addInterceptor(new LoggerInterceptor("xxx",true));
//            okHttpBuilder.retryOnConnectionFailure(false);

            //必须是设置Cache目录
//            Cache cache = new Cache(mContext.getCacheDir(), 10 * 1024 * 1024);
//            okHttpBuilder.cache(cache);
            //time out
//            okHttpBuilder .readTimeout(TIMEOUT_READ, TimeUnit.SECONDS);
//            okHttpBuilder.connectTimeout(TIMEOUT_CONNECTION, TimeUnit.SECONDS);
            //走缓存，两个都要设置
//            okHttpBuilder.addInterceptor(new OnOffLineCachedInterceptor());
//            okHttpBuilder.addNetworkInterceptor(new OnOffLineCachedInterceptor());
//            okHttpBuilder.addInterceptor(new LoggingInterceptor());
//            okHttpBuilder.addNetworkInterceptor(
//                    new HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.HEADERS));

            SSLParams sslParams = HttpsUtils.getSslSocketFactory(null, ins, pwd);
            okHttpBuilder.sslSocketFactory(sslParams.sSLSocketFactory,sslParams.trustManager);
//                    .hostnameVerifier(new HostnameVerifier() {
//                @Override
//                public boolean verify(String hostname, SSLSession session) {
//                    return true;
//                }
//            });
            okHttpClient = okHttpBuilder.build();
            mApi = new  Retrofit.Builder().
                    baseUrl("https://test.wdieb.com/employeeBenefits/").
//                    addCallAdapterFactory(new ExecutorCallAdapterFactory()).
            addCallAdapterFactory(RxJavaCallAdapterFactory.create()).
            addConverterFactory(GsonConverterFactory.create()).
                            client(okHttpClient).
                            build().create(Api.class);
        }
    }


    public void addHeader( String key, String value){
        if (mHeaderInterceptor !=null){
            mHeaderInterceptor.addHeader(key,value);
        }
    }
    public void removeHeader( String key){
        if (mHeaderInterceptor !=null){
            mHeaderInterceptor.removeHeader(key);
        }
    }


    public Api getmApi() {
        if (null == mApi){

       /*     OkHttpClient okHttpClient = new OkHttpClient();
            OkHttpClient.Builder builder = okHttpClient.newBuilder();
            builder.retryOnConnectionFailure(false);
            mApi = new  Retrofit.Builder().
                    baseUrl("http://182.150.35.136/jfw-web/").
//                    addCallAdapterFactory(new ExecutorCallAdapterFactory()).
                    addConverterFactory(GsonConverterFactory.create()).
                    client(okHttpClient).
                    build().create(Api.class);*/
        }
        return mApi;
    }










//   class LoggingInterceptor implements Interceptor {
//        @Override public Response intercept(Interceptor.Chain chain) throws IOException {
//            Request request = chain.request();
//
//            long t1 = System.nanoTime();
//            Logger.e(String.format("Sending request %s on %s%n%s",
//                    request.url(), chain.connection(), request.headers()));
//
//            Response response = chain.proceed(request);
//
//            long t2 = System.nanoTime();
//            Logger.e(String.format("Received response for %s in %.1fms%n%s",
//                    response.request().url(), (t2 - t1) / 1e6d, response.headers()));
//
//            return response;
//        }
//    }





}
